<template>
    <div class="orderCont" :style="'min-height: '+docheight+'px'">
      <div v-if="!showTimeOut&&showCont">
        <div class="orderHead">
          <serch-com placher="请输入订单商品名称" @serchClick="serchFn" color="#916ce0"></serch-com>
          <div class="orderTop">
            <div class="orderNav">
              <div :class="stepId==item.id?'navItem_active':'navItem'" @click="navFn(item)" :key="index" v-for="(item,index) in navArr">{{item.val}}</div>
            </div>
          </div>
        </div>
        <div class="orderList" v-if="orderList.length>0">
          <div class="orderItem" :key="index" v-for="(item,index) in orderList">
            <div class="shopTop">
              <div class="shopNameCont">
                <div class="shopName">订单号:{{ item.order_number }}</div>
              </div>
              <div class="orderStatus">
                <span v-if="item.order_status==1">待付款</span>
                <span v-if="item.order_status==2">待接单</span>
                <span v-if="item.order_status==3">待服务</span>
                <span v-if="item.order_status==5">已完成</span>
                <span v-if="item.order_status==6">已关闭</span>
                <span v-if="item.order_status==7">已退款</span>
              </div>
            </div>
            <div class="goodsList">
              <div class="goodsItem" @click="toDetail(item)">
                <div class="goodsImg">
                  <img :src="item.package_cover+ossSrc">
                </div>
                <div class="goodsInfo">
                  <div class="goodsName">{{ item.package_name }}</div>
                  <div class="orderTime">预约时间：{{ item.service_time }}</div>
                  <div class="goodsType">
                    <div class="goodsTypeItem">服务保洁员：{{ item.technician_nick_name }}</div>
                  </div>
                  <div class="goodsInfoPrice">
                    <div>总价:¥{{ item.order_total_price }}</div>
                    <div>{{item.package_time }}分钟</div>
                  </div>
                </div>
              </div>
            </div>
            <div class="goodsCtrl">
              <div class="orderBtn greenBtn" v-if="item.order_status==1" @click="surePay(item)">立即支付</div>
              <div class="orderBtn blackBtn" v-if="item.order_status==1||item.order_status==2||item.order_status==3" @click="ctrlFn(item,1)">取消订单</div>
              <div class="orderBtn blackBtn" @click="ctrlFn(item,2)" v-if="(item.order_status==5&&item.is_report==1)||item.order_status==6">删除订单</div>
              <div class="orderBtn pinkBtn" @click="replayOrderFn(item)" v-if="item.order_status==5&&item.is_evaluate==1">评价订单</div>
            </div>
            <div class="orderDes" v-if="item.order_status==7">
              <div>退款金额：{{ item.refund_money }} </div>
              <div>退款说明：{{ item.close_reason }}</div>
            </div>
          </div>
        </div>
        <div class="loadMore" v-if="isChageNav">数据请求中~</div>
        <div class="loadMore" v-if="orderList.length>0&&!isChageNav">{{ loadMoreText }}</div>
        <nodata v-if="orderList.length==0&&!isChageNav" text="暂无该类订单~"></nodata>
        <van-popup v-model:show="showConfirm" round position="bottom">
          <div class="orderMask">
              <div class="maskTitle">{{ maskTitle }}</div>
              <div class="maskTip">{{ maskTipCont }}</div>
              <div class="maskTip Money">{{ maskTipMoney }}</div>
              <saveBtn color="#916ce0" style="width: 100%;margin-top: 0.75rem;" :value="maskBtnCont" @saveClick="sureMask"></saveBtn>
          </div>
        </van-popup>
      </div>
      <toast v-model="showToast" position="middle" width="50%" type="text" :time="1000">{{showText}}</toast>
      <getLoad v-if="!showCont"></getLoad>
      <getTimeOut v-if="showTimeOut"></getTimeOut>
      <sub-load v-if="showLoad"></sub-load>
      <footcpm></footcpm>
    </div>
  </template>
  
  <script>
  import nodata from '@/components/public/nodata'
  import serchCom from '@/components/public/serchCom.vue'
  import footcpm from '@/components/footcpm'
  import SaveBtn from "@/components/public/saveBtn.vue";

  import { orderList,orderCancel,orderPay,orderDel,orderCancelMsg } from '@/http/api'
  import {Toast,Confirm } from 'vux'
  import getLoad from '@/components/public/getLoad'
  import subLoad from '@/components/public/subLoad.vue'
  
  import getTimeOut from '@/components/public/getTimeOut'
  export default {
    components:{SaveBtn,footcpm,subLoad,getLoad,getTimeOut,nodata,serchCom,Toast,Confirm},
    data(){
      return{
        loadMoreText:'数据加载中~',
        isChageNav:false,
        showLoad:false,
        showTimeOut: false,
        showCont:false,
        navArr:[
          {val:'全部',id:0},
          {val:'待付款',id:1},
          {val:'待接单',id:2},
          {val:'待服务',id:3},
          {val:'已完成',id:5},
        ],
        showText:'',
        showToast:false,
        serchKey:'',
        stepId:0,
        per_page:10,
        page:1,
        orderList:[],
        
        showConfirm:false,
        activeOrder:{},
        activeIndex:0,
        // 分页
        getting:false,
        last_page:0,
        maskTitle:'',
        maskTipCont:'',
        maskTipMoney:'',
        maskBtnCont:'',
      }
    },
    methods:{
      surePay(item){
        let data = {
          order_id:item.order_id,
        }
        if(!this.showLoad){
          this.showLoad = true
          orderPay(data).then((res)=>{
            if(res.data.code==1){
              WeixinJSBridge.invoke('getBrandWCPayRequest', {
                appId: res.data.data.appId,   //公众号ID，由商户传入    
                timeStamp: res.data.data.timeStamp,   //时间戳，自1970年以来的秒数    
                nonceStr: res.data.data.nonceStr,      //随机串    
                package: res.data.data.package,
                signType: res.data.data.signType,     //微信签名方式：    
                paySign: res.data.data.paySign, //微信签名
            },
            (resData)=>{
                if (resData.err_msg == "get_brand_wcpay_request:ok") {
                    // 使用以上方式判断前端返回,微信团队郑重提示：
                    //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    this.showToast = true
                    this.showText = '支付成功'
                    this.page = 1
                    this.last_page = 0
                    this.orderList = []
                    this.getData()
                }else{
                    this.showToast = true
                    this.showText = '支付失败'
                }
            })
            }else{
              this.showToast = true
              this.showText =res.data.msg
            }
            this.showLoad = false
          }).catch(()=>{
            this.showLoad = false
            this.showToast = true
            this.showText = '网络请求超时'
          })
        }
      },
      ctrlFn(item,num){
        this.activeIndex = num
        this.activeOrder = item
        if(num==1){
          if(item.order_status!=1){
            this.maskTitle = '取消订单提示'
            this.maskBtnCont = '确认取消'
            this.getCancelMsg(item)
          }else{
            this.canceFn(item)
          }
        }else if(num==2){
          this.maskTitle = '删除订单提示'
          this.maskTipCont = '确定要删除此订单吗？订单删除后将无法恢复。'
          this.maskBtnCont = '确认删除'
          this.showConfirm = true
        }
      },
      sureMask(){
        if(this.activeIndex == 1){
          this.canceFn(this.activeOrder)
        }else if(this.activeIndex ==2){
          this.delFn(this.activeOrder)
        }
      },
      getCancelMsg(item){
        let data ={
          order_id:item.order_id
        }
        orderCancelMsg(data).then((res)=>{
            if(res.data.code==1){
              this.maskTipCont = '退款说明：'+res.data.data.msg
              this.maskTipMoney = '退款金额：'+res.data.data.money+'元'
              this.showConfirm = true
            }else{
              this.showToast = true
              this.showText = res.data.msg
            }
            this.showLoad = false
          }).catch(()=>{
            this.showLoad = false
            this.showToast = true
            this.showText = '网络请求超时'
          })
      },
      delFn(item){
        let data ={
          order_id:item.order_id
        }
        this.showConfirm = false
        if(!this.showLoad){
          this.showLoad = true
          orderDel(data).then((res)=>{
            if(res.data.code==1){
              this.orderList.splice(this.activeIndex,1)
              this.showToast = true
              this.showText = '订单删除成功'
            }else{
              this.showToast = true
              this.showText = res.data.msg
            }
            this.showLoad = false
          }).catch(()=>{
            this.showLoad = false
            this.showToast = true
            this.showText = '网络请求超时'
          })
        }
      },
      canceFn(item){
        let data ={
          order_id:item.order_id
        }
        if(!this.showLoad){
          this.showLoad = true
          orderCancel(data).then((res)=>{
            if(res.data.code==1){
              if(this.stepId==0){
                if(item.order_status==1){
                  item.order_status=6
                }else{
                  item.order_status=7
                  item.close_reason = this.maskTipCont.split('退款说明：')[1]
                  item.refund_money = this.maskTipMoney.split('退款金额：')[1]
                  this.showConfirm = false
                }
              }else{
                this.orderList.splice(this.activeIndex,1)
              }
              this.showToast = true
              this.showText = '订单取消成功'
            }else{
              this.showToast = true
              this.showText = '订单取消失败'
            }
            this.showLoad = false
          }).catch(()=>{
            this.showLoad = false
            this.showToast = true
            this.showText = '网络请求超时'
          })
        }
      },
      serchFn(val){
        this.serchKey = val
        this.page = 1
        this.last_page = 0
        this.orderList = []
        this.getData()
      },
      replayOrderFn(item){
        this.$router.push({path:'/replayOrder',query:{id:item.order_id}})
      },
      navFn(item){
        this.stepId = item.id
        this.orderList = []
        this.page = 1
        this.getData()
      },
      toShop(item) {
        this.$router.push({path:'/shopDetail',query:{id:item.shopping_shop_id}})
      },
      toDetail(item){
        this.$router.push({path:'/orderDetail',query:{id:item.order_id}})
      },
      getData(){
        let data ={
          order_status:this.stepId,
          search:this.serchKey,
          per_page:this.per_page,
          page:this.page,
        }
        this.isChageNav = true
        orderList(data).then((res)=>{
          if(res.data.code==1){
            let arr = res.data.data.data
            for(let i=0;i<arr.length;i++){
              this.orderList.push(arr[i])
            }
            setTimeout(() => {
              this.getting = false
            }, 100);
            this.last_page = res.data.data.last_page
            if(this.page==this.last_page){
              this.loadMoreText = '到底啦～'
            }else{
              this.loadMoreText = '数据加载中~'
            }
          }else{
            this.showToast = true
            this.showText = '订单获取失败'
          }
          this.showCont= true
          this.isChageNav = false
  
        }).catch(()=>{
          this.showCont = true
          this.showTimeOut = true
        })
      },
      nextfn(){
        if(this.$route.path == '/order'){
          let h = document.documentElement.scrollHeight||document.body.scrollHeight
          let wh = document.documentElement.clientHeight||document.body.clientHeight
          let top = document.documentElement.scrollTop||document.body.scrollTop
          let bottom = h-wh-top
          if(bottom<=50){
            if(this.getting==false){
              if(this.last_page>this.page){
                this.getting=  true
                this.page++
                this.getData()
              }
            }
          }
        }
      }
    },
    mounted() {
      window.addEventListener('scroll',this.nextfn)
    },
    created() {
      this.getData()
    }
  }
  </script>
  
  <style scoped lang="less">
  .orderCont{
    width: 100%;
    background-color: #F2F3F3;
    box-sizing: border-box;
    padding-bottom: 3.25rem;
    padding-top: 6.5rem;
    .orderHead{
      width: 100%;
      background-color: #fff;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 100;
      
      .orderTop{
        width: 100%;
        padding: 0 4%;
        box-sizing: border-box;
        background-color: #fff;
        .orderNav{
          width: 100%;
          display: flex;
          position: relative;
          justify-content: space-between;
          .navItem,.navItem_active{
            width: 20%;
            text-align: center;
            padding: 0.75rem;
            box-sizing: border-box;
            font-size: 0.75rem;
            color: #666;
            white-space: nowrap;
          }
          .navItem_active{
            color: #916ce0;
            border-bottom: 1px solid #916ce0;
          }
        }
      }
    }
    .orderList{
      width: 100%;
      padding: 0.25rem 4%;
      box-sizing: border-box;
      .orderItem{
        width: 100%;
        background-color: #fff;
        margin-bottom: 0.75rem;
        border-radius: 0.25rem;
        padding: 0 0.75rem 1rem;
        box-sizing: border-box;
        .shopTop{
          display: flex;
          width: 100%;
          align-items: center;
          justify-content: space-between;
          .shopNameCont{
            flex: 1;
            width: 0.5rem;
            display: flex;
            padding: 0.75rem 0;
            align-items: center;
            margin-right: 0.5rem;
            .shopicon{
              font-size: 1.1rem;
              margin-right: 0.25rem;
            }
            .shopName{
              font-size: 0.7rem;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
            }
            .icon-jinrujiantou{
              font-size: 0.8rem;
              position: relative;
            }
          }
          .orderStatus{
            font-size: 0.6rem;
            color: #916ce0;
          }
        }
        .goodsList{
          width: 100%;
          .goodsItem{
            width: 100%;
            margin-bottom: 0.5rem;
            display: flex;
            align-items: center;
            .goodsImg{
              width: 5rem;
              height: 5rem;
              overflow: hidden;
              border-radius: 0.25rem;
              margin-right: 0.5rem;
              img{
                width: 100%;
              }
            }
            .goodsInfo{
              width: 0.5rem;
              flex: 1;
              display: flex;
              flex-direction: column;
              height: 5rem;
              justify-content: space-between;
              .goodsName{
                width: 100%;
                line-height: 1rem;
                font-size: 0.7rem;
                overflow: hidden; //超出的文本隐藏
                text-overflow: ellipsis; //溢出用省略号显示
                white-space: nowrap;
              }
              .orderTime{
                font-size: 0.6rem;
                color: #666;
              }
              .goodsType{
                display: flex;
                align-items: center;
                justify-content: space-between;
                .goodsNumCont{
                  display: flex;
                  height: 1rem;
                  line-height: 1rem;
                  color: #999;
                  font-size: 0.6rem;
                  align-items: center;
                }
                .goodsTypeItem{
                  padding:0.1rem 0.5rem;
                  background-color: #F6F6F6;
                  font-size: 0.6rem;
                  height: 1rem;
                  line-height: 1rem;
                  border-radius: 0.15rem;
                  color: #999;
                }
              }
              .goodsInfoPrice{
                color: #999;
                font-size: 0.6rem;
                display: flex;
                justify-content: space-between;
                align-items: center;
              }
            }
          }
        }
        .orderSumCont{
          font-size: 0.6rem;
          text-align: right;
          color: #333;
          .priceVal{
            position: relative;
            top: 0.1rem;
            color: #916ce0;
          }
        }
        .orderTip{
          font-size: 0.6rem;
          color: #999;
          text-align: right;
          margin-top: 0.5rem;
        }
        .goodsCtrl{
          width: 100%;
          display: flex;
          align-items: center;
          margin-top: 0.75rem;
          justify-content: flex-end;
          .orderBtn{
            padding: 0.35rem 0;
            width: 25%;
            text-align: center;
            border-radius: 1rem;
            font-size: 0.6rem;
            box-sizing: border-box;
            margin-left: 0.5rem;
          }
          .greenBtn{
            border: 1px solid #916ce0;
            color:#916ce0;
          }
          .pinkBtn{
            border: 1px solid #916ce0;
            color: #916ce0;
          }
          .blackBtn{
            border: 1px solid #999;
            color: #666;
          }
        }
        .orderDes{
          border-top: 1px dashed #ebebeb;
          padding-top: 0.75rem;
          color: #666;
          font-size: 0.6rem;
        }
      }
    }
    .loadMore{
      text-align: center;
      padding: 0.75rem 0;
      font-size: 0.6rem;
      color: #999;
    }
    .orderMask{
    padding: 0.75rem;
    box-sizing: border-box;
    .maskTitle{
      font-size: 0.8rem;
      font-weight: bold;
      text-align: center;
    }
    .maskTip{
      margin-top: 0.5rem;
      line-height: 1.1rem;
    }
  }
  }
  </style>
  